package com.woniu.adopt_web.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt_web.entity.Pet;
import com.woniu.adopt_web.entity.PetAdopt;
import com.woniu.adopt_web.entity.PetCollection;
import com.woniu.adopt_web.entity.RescueStation;
import com.woniu.adopt_web.service.PetService;
import com.woniu.adopt_web.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("pet")
public class PetController {
    @Autowired
    private PetService petService;


    @GetMapping("/list")
    public String list(@RequestParam(value = "now", defaultValue = "1") Integer now,
                       @RequestParam(value = "size", defaultValue = "3") Integer size,
                       @RequestParam(value = "category", defaultValue = "0") Integer category,
                       @RequestParam(value = "sex", defaultValue = "0") Integer sex,
                       @RequestParam(value = "city" ,defaultValue = "0") String city,
                       Model model, HttpSession session) {
        PageInfo<Pet> petPageInfo = petService.queryByPage(now, size,category,sex,city);
        model.addAttribute("pagePet", petPageInfo);
        Pet pet = new Pet();
        pet.setCategory(category);
        pet.setSex(sex);
        model.addAttribute("pet", pet);
//        UserVo loginUser = (UserVo) session.getAttribute("loginUser");
//        if(loginUser!=null){
//            session.setAttribute("User", loginUser);
//        }else {
//            UserVo userVo = new UserVo();
//            userVo.setId(0);
//            session.setAttribute("User", userVo);
//        }
        model.addAttribute("city",city);
        model.addAttribute("cityList",petService.getListCity());
        return "petAdopt";
    }


    @RequestMapping("petDetail")
    public String petDetail(Integer id, Model model) {
        Pet pet = petService.queryPetByKey(id);
        RescueStation rescueStation=petService.findStationById(pet.getResId());
        model.addAttribute("pet", pet);
        model.addAttribute("rescueStation", rescueStation);
        return "petDetail";
    }

    @ResponseBody
    @RequestMapping("/adopt")
    public int adopt(@RequestParam(value = "uid") int uid, @RequestParam(value = "pid") int pid) {
        System.out.println("用户id"+uid+"====宠物id"+pid);
        PetAdopt adopt = petService.findAdoptByUidAndPid(uid, pid);
        if (adopt==null) {
            int i=petService.addAdopt(uid,pid);
            return 1;
        } else {
            return 0;
        }
    }
    @ResponseBody
    @RequestMapping("/collection")
    public int collection(@RequestParam(value = "uid") int uid, @RequestParam(value = "pid") int pid){
        System.out.println("用户id"+uid+"====宠物id"+pid);
        PetCollection collectionByUidAndPid = petService.findCollectionByUidAndPid(uid, pid);
        if(collectionByUidAndPid==null){
            return petService.addCollection(uid,pid);
        }else {
            return 0;
        }
    }

    @ResponseBody
    @RequestMapping("/collectionDel")
    public int collectionDel(@RequestParam(value = "uid") int uid, @RequestParam(value = "pid") int pid){
        PetCollection collectionByUidAndPid = petService.findCollectionByUidAndPid(uid, pid);
        int i = petService.removeCollection(collectionByUidAndPid.getId());
        if(i==1){
            return 1;
        }else {
            return 0;
        }
    }
}
